package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.Node;
import com.cy.pj.sys.dao.SysMenuDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.entity.SysMenu;
import com.cy.pj.sys.entity.SysUserMenu;
import com.cy.pj.sys.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public List<Map<String, Object>> findObjects() {
        long begin = System.currentTimeMillis();
        List<Map<String, Object>> list = sysMenuDao.findObjects();
        long end = System.currentTimeMillis();
        log.info("use time {}", end - begin);
        if(list == null || list.size() == 0)
            throw new ServiceException("没有对应菜单的信息");
        return list;
    }

    @Override
    public List<Node> findZtreeMenuNodes() {
        return sysMenuDao.findZtreeMenuNodes();
    }

    @Override
    public int saveObject(SysMenu entity) {
        if(entity == null)
            throw new IllegalArgumentException("保存对象不能为空");
        return sysMenuDao.insertObject(entity);
    }

    @Override
    public int updateObject(SysMenu entity) {
        if(entity == null)
            throw new ServiceException("保存对象不能为空");
        int rows = sysMenuDao.updateObject(entity);
        if(rows == 0)
            throw new ServiceException("记录可能已经不存在");
        return rows;
    }

    @Override
    public int deleteObject(Integer id) {
        // 1、验证数据的合法性
        if(id == null || id <= 0)
            throw new IllegalArgumentException("请先选择");
        // 2、基于ID进行子菜单查询
        int count = sysMenuDao.getChildCount(id);
        if(count > 0)
            throw new ServiceException("请先删除子菜单");
        // 3、删除角色菜单关系(sys_role_menus)
        sysRoleMenuDao.deleteObjectsByMenuId(id);
        // 4、删除菜单
        int rows = sysMenuDao.deleteObjects(id);
        if(rows == 0)
            throw new ServiceException("此菜单可能已经不存在");
        // 5、返回结果
        return rows;
    }

    @Override
    public List<SysUserMenu> findUserMenusByUserId(Integer id) {
        // 1、参数验证
        if(id == null || id < 1)
            throw new IllegalArgumentException("无效的用户ID");
        // 2、根据用户ID获取角色ID集
        List<Integer> roleIds = sysUserRoleDao.findRoleIdsByUserId(id);
        if(roleIds == null || roleIds.size() == 0)
            throw new ServiceException("用户未分配角色");
        // 3、根据角色ID集获取角色菜单ID集
        List<Integer> menuIds = sysRoleMenuDao.findMenuIdsByRoleIds(roleIds);
        if(menuIds == null || menuIds.size() == 0)
            throw new ServiceException("用户未分配权限");
        // 4、根据菜单ID集获取分级列表
        List<SysUserMenu> result = sysMenuDao.findMenusByIds(menuIds);
        return result;
    }
}
